package com.crm.service.system.impl;

import com.crm.context.BaseContext;
import com.crm.dto.QueryDictDataDto;
import com.crm.exception.BaseException;
import com.crm.mapper.system.DictDataMapper;
import com.crm.mapper.system.DictTypeMapper;
import com.crm.pojo.Dict;
import com.crm.pojo.DictData;
import com.crm.pojo.Referredsubject;
import com.crm.pojo.User;
import com.crm.service.system.DictDataService;
import com.crm.vo.ActivityStatusVO;
import com.crm.vo.ChannelTypeVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName DictDataServiceImpl
 * @Description
 * @Author 19632
 * @Date 2024/10/28 14:39
 * @Version
 **/
@Service
public class DictDataServiceImpl implements DictDataService {
    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private DictTypeMapper dictTypeMapper;

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public Page<DictData> queryAll(QueryDictDataDto dto) {

        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<DictData>page=dictDataMapper.page(dto);
        return page;
    }
    /**
     * 新增
     */
    @Override
    public void add(DictData dictData) {
        String dictType = dictData.getDictType();
        Dict dict=dictTypeMapper.getByDictType(dictType);
        Integer dictId = dict.getDictId();
        dictData.setDictId(dictId);
        dictData.setStatus("1");
        Long userId = BaseContext.getCurrentId();
        User user =dictTypeMapper.searchByUserId(userId);
        dictData.setCreateBy(user.getName());
        dictDataMapper.add(dictData);
    }

    @Override
    public DictData getByDictCode(Integer dictCode) {
        return dictDataMapper.getByDictCode(dictCode);
    }

    @Override
    public void update(DictData dictData) {
        Long userId = BaseContext.getCurrentId();
        User user = dictTypeMapper.searchByUserId(userId);
        dictData.setUpdateBy(user.getName());
        dictDataMapper.update(dictData);
    }

    @Override
    public void delete(List<Integer> dictCodes) {
        List<DictData> dictDataList = dictDataMapper.searchByDictCodes(dictCodes);
        for (DictData dictData : dictDataList) {
            if (("1").equals(dictData.getStatus())) {
                throw new BaseException("正常状态,不可删除!!");
            }
            Integer dictCode = dictData.getDictCode();
            dictDataMapper.delete(dictCode);
        }
    }


@Override
    public List<Referredsubject> getType(String type) {
        Dict dict = dictTypeMapper.getByDictType(type);
        if (dict == null) {
            throw new BaseException("查无字典类型!!");
        }
        Integer dictId = dict.getDictId();
        List<Referredsubject> referredsubjectList = dictDataMapper.getByid(dictId);
        return referredsubjectList;
    }

}
